﻿/*
This file is part of nDoctor Light

NDoctor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

NDoctor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Probel.Ldoc.ViewModels.ToolBoxes
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Dto;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;
    using Probel.Mvvm.Gui;

    public class RecordHistoryViewModel : BaseViewModel
    {
        #region Fields

        private readonly RecordCardDto Record;
        private readonly ICommand revertCommand;
        private readonly IPatientService Service;

        private RecordStateCardDto selectedState;

        #endregion Fields

        #region Constructors

        public RecordHistoryViewModel(IErrorService errorService, IPatientService patientService, RecordCardViewModel record, bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.Record = new RecordCardDto() { Id = record.Id };
            this.Service = patientService;
            this.Dates = new ObservableCollection<RecordStateCardDto>();
            this.revertCommand = new RelayCommand(this.Revert, this.CanRevert);
        }

        #endregion Constructors

        #region Properties

        public ObservableCollection<RecordStateCardDto> Dates
        {
            get;
            private set;
        }

        public ICommand RevertCommand
        {
            get { return this.revertCommand; }
        }

        public RecordStateCardDto SelectedState
        {
            get { return this.selectedState; }
            set
            {
                this.selectedState = value;
                this.OnPropertyChanged(() => SelectedState);
            }
        }

        #endregion Properties

        #region Methods

        public string GetStateRtf()
        {
            return this.Service.GetRtfFromRecordState(this.SelectedState);
        }

        public void Refresh()
        {
            var dates = this.Service.GetHistoryOf(this.Record);
            this.Dates.Refill(dates);
        }

        private bool CanRevert()
        {
            return this.Record != null;
        }

        private void Revert()
        {
            var revert = ViewService.MessageBox.Question(Messages.Msg_AskRevertRecordState);
            if (revert)
            {
                this.Service.SaveRtf(this.Record, this.GetStateRtf());
            }
        }

        #endregion Methods
    }
}